package com.weavernorth.salary;


import com.weavernorth.humanresource.SalarRangeUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;

import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.Iterator;

/**
 * 电商客服绩效流程推送SAP需求
 */
public class OnlineToSAP implements Action {

    public static Log log = LogFactory.getLog(OnlineToSAP.class.getName());


    @Override
    public String execute(RequestInfo requestInfo) {
        BaseBean bb = new BaseBean();
        //数据库操作
        RecordSet rs = new RecordSet();
        RecordSet rsDetail1 = new RecordSet();
        RecordSet rsDetail2 = new RecordSet();
        RecordSet rsDetail3 = new RecordSet();
        //当前requestid
        String strRequestid = requestInfo.getRequestid();
        //推送
        SalarRangeUtil salarRangeUtil = new SalarRangeUtil();
        //当前表名
        String strTable = requestInfo.getRequestManager().getBillTableName();
        //推送xml
        StringBuffer strRequestXml = new StringBuffer();
        strRequestXml.append("<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:urn='urn:sap-com:document:sap:rfc:functions'>");

        strRequestXml.append("<soapenv:Header/>");
        strRequestXml.append("<soapenv:Body>");
        strRequestXml.append("<urn:ZHRI_078>");
        strRequestXml.append("<RETURN>");

        strRequestXml.append("<item>");
        strRequestXml.append("<TYPE></TYPE>");
        strRequestXml.append("<ID></ID>");
        strRequestXml.append("<NUMBER></NUMBER>");
        strRequestXml.append("<MESSAGE></MESSAGE>");
        strRequestXml.append("<LOG_NO></LOG_NO>");
        strRequestXml.append("<LOG_MSG_NO></LOG_MSG_NO>");
        strRequestXml.append("<MESSAGE_V1></MESSAGE_V1>");
        strRequestXml.append("<MESSAGE_V2></MESSAGE_V2>");
        strRequestXml.append("<MESSAGE_V3></MESSAGE_V3>");
        strRequestXml.append("<MESSAGE_V4></MESSAGE_V4>");
        strRequestXml.append("<PARAMETER></PARAMETER>");
        strRequestXml.append("<ROW></ROW>");
        strRequestXml.append("<FIELD></FIELD>");
        strRequestXml.append("<SYSTEM></SYSTEM>");
        strRequestXml.append("</item>");
        strRequestXml.append("</RETURN>");
        strRequestXml.append("<T_9136>");


        //查询当前数据
        String sql = "select * from " + strTable + " where requestid=?";
        log.info("查询sql:" + sql + ";requestid:" + strRequestid);
        rs.executeQuery(sql, strRequestid);
        if (rs.next()) {
            //评分类型
            String strPflx = Util.null2String(rs.getString("pflx"));
            log.info("评分类型:" + strPflx);
            //id
            String strId = Util.null2String(rs.getString("id"));
            //评分日期
            String strPfrq = Util.null2String(rs.getString("pfrq"));
            //售前客服绩效 获取明细表1的信息
            //if ("0".equals(strPflx)) {
            String detailSql1 = "select * from " + strTable + "_dt1 where mainid=?";
            log.info("售前客服绩效的sql:" + detailSql1 + ";strId:" + strId);
            //}
            rsDetail1.executeQuery(detailSql1, strId);
            while (rsDetail1.next()) {
                //员工编号
                String strYgbh = Util.null2String(rsDetail1.getString("ygbh"));
                //员工姓名
                String strXm = Util.null2String(rsDetail1.getString("ygxm"));

                //zm 增加,判断工资范围是否锁定 开始
                String strGzfw = salarRangeUtil.getGzfwFromUser(strXm);

                String strLockMess = salarRangeUtil.judgeLock(strGzfw, strPfrq);
                log.info("工资核算范围返回信息:" + strLockMess);
                if (!"".equals(strLockMess)) {
                    strLockMess = "工号为:" + strYgbh + "," + strLockMess + "薪资核算结束后，工资范围解除锁定，流程可以继续提交";
                    requestInfo.getRequestManager().setMessagecontent(strLockMess);
                    return FAILURE_AND_CONTINUE;

                }
                //zm 增加,判断工资范围是否锁定 结束
                //总得分
                String strDfhj = Util.null2String(rsDetail1.getString("dfhj"));
                //奖金金额
                String strJjje = Util.null2String(rsDetail1.getString("jjje"));
                strRequestXml.append("<item>");
                strRequestXml.append("<PERNR>" + strYgbh + "</PERNR >");
                strRequestXml.append("<ZJXPF>" + strDfhj + "</ZJXPF >");
                strRequestXml.append("<ZJX_DATE>" + strPfrq + "</ZJX_DATE >");
                strRequestXml.append("<ZJE>" + strJjje + "</ZJE >");
                strRequestXml.append("<ZTYPE>20</ZTYPE>");
                strRequestXml.append("</item >");
            }
            //售后客服绩效 获取明细表2的信息
            String detailSql2 = "select * from " + strTable + "_dt2 where mainid=?";
            log.info("售后客服绩效的sql:" + detailSql2 + ";strId:" + strId);
            rsDetail2.executeQuery(detailSql2, strId);
            while (rsDetail2.next()) {
                //员工编号
                String strYgbh = Util.null2String(rsDetail2.getString("ygbh"));
                //员工姓名
                String strXm = Util.null2String(rsDetail2.getString("ygxm"));

                //zm 增加,判断工资范围是否锁定 开始
                String strGzfw = salarRangeUtil.getGzfwFromUser(strXm);

                String strLockMess = salarRangeUtil.judgeLock(strGzfw, strPfrq);
                log.info("工资核算范围返回信息:" + strLockMess);
                if (!"".equals(strLockMess)) {
                    strLockMess = "工号为:" + strYgbh + "," + strLockMess + "薪资核算结束后，工资范围解除锁定，流程可以继续提交";
                    requestInfo.getRequestManager().setMessagecontent(strLockMess);
                    return FAILURE_AND_CONTINUE;

                }
                //zm 增加,判断工资范围是否锁定 结束
                //总得分
                String strDfhj = Util.null2String(rsDetail2.getString("dfhj"));
                //奖金金额
                String strJjje = Util.null2String(rsDetail2.getString("jjje"));
                strRequestXml.append("<item>");
                strRequestXml.append("<PERNR>" + strYgbh + "</PERNR >");
                strRequestXml.append("<ZJXPF>" + strDfhj + "</ZJXPF >");
                strRequestXml.append("<ZJX_DATE>" + strPfrq + "</ZJX_DATE >");
                strRequestXml.append("<ZJE>" + strJjje + "</ZJE >");
                strRequestXml.append("<ZTYPE>20</ZTYPE>");
                strRequestXml.append("</item >");
            }
            //售后客服绩效 获取明细表2的信息
            String detailSql3 = "select * from " + strTable + "_dt3 where mainid=?";
            log.info("售后客服绩效的sql:" + detailSql3 + ";strId:" + strId);
            rsDetail3.executeQuery(detailSql3, strId);
            while (rsDetail3.next()) {
                //员工编号
                String strYgbh = Util.null2String(rsDetail3.getString("ygbh"));
                //员工姓名
                String strXm = Util.null2String(rsDetail3.getString("ygxm"));

                //zm 增加,判断工资范围是否锁定 开始
                String strGzfw = salarRangeUtil.getGzfwFromUser(strXm);

                String strLockMess = salarRangeUtil.judgeLock(strGzfw, strPfrq);
                log.info("工资核算范围返回信息:" + strLockMess);
                if (!"".equals(strLockMess)) {
                    strLockMess = "工号为:" + strYgbh + "," + strLockMess + "薪资核算结束后，工资范围解除锁定，流程可以继续提交";
                    requestInfo.getRequestManager().setMessagecontent(strLockMess);
                    return FAILURE_AND_CONTINUE;

                }
                //zm 增加,判断工资范围是否锁定结束
                //总得分
                String strDfhj = Util.null2String(rsDetail3.getString("dfhj"));
                //奖金金额
                String strJjje = Util.null2String(rsDetail3.getString("jjje"));
                strRequestXml.append("<item>");
                strRequestXml.append("<PERNR>" + strYgbh + "</PERNR >");
                strRequestXml.append("<ZJXPF>" + strDfhj + "</ZJXPF >");
                strRequestXml.append("<ZJX_DATE>" + strPfrq + "</ZJX_DATE >");
                strRequestXml.append("<ZJE>" + strJjje + "</ZJE >");
                strRequestXml.append("<ZTYPE>20</ZTYPE>");
                strRequestXml.append("</item >");
            }
        }
        strRequestXml.append("</T_9136>");
        strRequestXml.append("</urn:ZHRI_078>");
        strRequestXml.append("</soapenv:Body>");
        strRequestXml.append("</soapenv:Envelope>");
        log.info("电商激励推送的xml:" + strRequestXml.toString());
        //获取电商激励地址
        String strDsjlUrl = Util.null2String(bb.getPropValue("personnel-erdos", "dsjxurl"));

        try {
            String responseXml = salarRangeUtil.sendPost(strDsjlUrl, strRequestXml.toString(), "utf-8");
            log.info("电商客户激励返回xml:" + responseXml);
            //返回标识
            String strReturnStatus = "";
            //返回信息
            String strMessage = "";
            Document doc = null;//解析返回值开始

            doc = DocumentHelper.parseText(responseXml);
            Iterator<Element> its2 = doc.selectNodes("//Type").iterator();
            while (its2.hasNext()) {
                Element element = its2.next();
                strReturnStatus = element.getText();
                if (!"".equals(strReturnStatus)) {
                    break;
                }
            }

            bb.writeLog("返回值:" + strReturnStatus);

            //返回值失败时,则提示失败
            if ("E".equals(strReturnStatus)) {
                Iterator<Element> its3 = doc.selectNodes("//Message").iterator();
                while (its3.hasNext()) {
                    Element element2 = its3.next();
                    strMessage = element2.getText();
                    if (!"".equals(strMessage)) {
                        break;
                    }
                }

            }

            if ("E".equals(strReturnStatus)) {
                requestInfo.getRequestManager().setMessagecontent(strMessage);
                return FAILURE_AND_CONTINUE;
            }


        } catch (Exception e) {
            e.printStackTrace();
        }
        return SUCCESS;

    }
}







